package com.xxxcompany.blog.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xxxcompany.blog.entity.User;
import com.xxxcompany.blog.service.IUserService;
import com.xxxcompany.blog.service.impl.UserServiceImpl;

@WebServlet("/loginchk")
public class LoginCheckServlet extends HttpServlet {
	
	IUserService userService = new UserServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		// 1. 获取用户请求信息
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		// 2.调用service处理
		User u = userService.login(username, password);
		
		// 3. 打印结果或者跳转制定页面
		if(u!=null)
		{
			HttpSession session = req.getSession();
			session.setAttribute("user", u);
//			pw.println("登陆成功");
			
			// 跳转到首页去
			resp.sendRedirect("index");
		}
		else
		{
			pw.println("登陆失败");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}